package com.xinxing.city.recoverycheck.ui;

import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.databinding.DataBindingUtil;

import com.xinxing.city.recoverycheck.DI;
import com.xinxing.city.recoverycheck.R;

import com.xinxing.city.recoverycheck.databinding.ActivityDoorTestBinding;
import com.xinxing.city.recoverycheck.interfaces.IDeviceKernel;

public class DoortestActivity extends AppCompatActivity {
    private ActivityDoorTestBinding binding;
    private IDeviceKernel deviceKernel;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = DataBindingUtil.setContentView(this, R.layout.activity_door_test);
        deviceKernel = DI.getInstance(IDeviceKernel.class);
        binding.kg.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                deviceKernel.getSerial().openDoor(1);
                handler.sendEmptyMessageDelayed(1,3000);
            }
        });
        binding.back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                DoortestActivity.this.finish();
                handler.removeMessages(1);
                handler.removeMessages(2);
            }
        });
        binding.zip.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                deviceKernel.getSerial().openZip(1);
            }
        });
    }
    Handler handler = new Handler(Looper.myLooper()){
        @Override
        public void handleMessage(@NonNull Message msg) {
            super.handleMessage(msg);
            switch (msg.what){
                case 1:
                    deviceKernel.getSerial().closeDoor(1);
                    handler.sendEmptyMessageDelayed(2,3000);
                    break;
                case 2:
                    deviceKernel.getSerial().openDoor(1);
                    handler.sendEmptyMessageDelayed(1,3000);
                    break;
            }
        }
    };
}
